package org.common.rsa;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.common.constants.RSAConstants;
import org.common.redis.RedisClient;
import org.common.utils.CommonUtils;

import java.security.*;

public class RSAKeyCreater {

	private static final String ALE_ENCODED = "RSA";
	private static final String ALE_ENCODED_2 = "BC";
	private static final int INIT_SIZE = 1024;
	private static KeyPairGenerator keyPairGenerator;
	private static KeyPair keyPair;
	private static PublicKey publicKey;
	private static PrivateKey privateKey;
	private static String publicKeyFilePath;
	private static String privateKeyFilePath;

	public static String[] createKeyPairs() {
		Security.addProvider((Provider) new BouncyCastleProvider());
		String strPK = "";
		String strPrik = "";
		try {
			(RSAKeyCreater.keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC")).initialize(1024,
					new SecureRandom());
			RSAKeyCreater.keyPair = RSAKeyCreater.keyPairGenerator.generateKeyPair();
			RSAKeyCreater.publicKey = RSAKeyCreater.keyPair.getPublic();
			RSAKeyCreater.privateKey = RSAKeyCreater.keyPair.getPrivate();
			final byte[] pubKey = RSAKeyCreater.publicKey.getEncoded();
			final byte[] priKry = RSAKeyCreater.privateKey.getEncoded();
			strPK = Base64.encodeBase64String(pubKey);
			strPrik = Base64.encodeBase64String(priKry);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e2) {
			e2.printStackTrace();
		}
		return new String[] { strPK, strPrik };
	}

	public static void createPrivateKeyFile(final String key, final String filePath) {
		CommonUtils.outputStreamWriter(key, filePath);
	}
	
	public static void createPublicKeyFile(final String key, final String filePath) {
		CommonUtils.outputStreamWriter(key, filePath);
    }
	
	public static String createPublicKeyRedis(final String key) {
		RedisClient redisClient = new RedisClient();
		return redisClient.keySet(RSAConstants.PUBLIC_CONSTANTS, key);
	}
	
	public static String createPrivateKeyRedis(final String key) {
		RedisClient redisClient = new RedisClient();
		return redisClient.keySet(RSAConstants.PRIVATE_CONSTANTS, key);
	}
	
	public static String getPublicKeyFile() {
		return CommonUtils.getKeyFile(RSAKeyCreater.publicKeyFilePath);
    }
	
	public static String getPrivateKeyFile() {
		return CommonUtils.getKeyFile(RSAKeyCreater.privateKeyFilePath);
    }
	
	public static String getPublicKeyRedis() {
		RedisClient redisClient = new RedisClient();
		return redisClient.keyGetKey(RSAConstants.PUBLIC_CONSTANTS);
	}
	
	public static String getPrivateKeyRedis() {
		RedisClient redisClient = new RedisClient();
		return redisClient.keyGetKey(RSAConstants.PRIVATE_CONSTANTS);
	}
	
	public static void main(final String[] args) {
//		String[] keys = createKeyPairs();
//		System.out.println(createPublicKeyRedis(keys[0]));
//		System.out.println(createPrivateKeyRedis(keys[1]));
		System.out.println(getPublicKeyRedis());
		System.out.println(getPrivateKeyRedis());
    }
	
	static {
        RSAKeyCreater.keyPairGenerator = null;
        RSAKeyCreater.publicKeyFilePath = "C:\\eclipse-project\\common\\src\\main\\java\\com\\common\\file\\publicKey.key";
        RSAKeyCreater.privateKeyFilePath = "C:\\eclipse-project\\common\\src\\main\\java\\com\\common\\file\\privateKey.key";
    }
}
